import { Component } from '@angular/core'
import { WebSiteGridContext, WebSiteGridUtils } from '@share/submodule/grid/index'
import { ColDef } from 'ag-grid-community'
import { AdCellTemplateComponent } from 'monk-design/grid'
@Component({
  selector: '{{selector}}',
  template: `
    <ag-grid-angular
      [style.height.px]="200"
      class="ag-theme-material"
      [gridOptions]="gridContext.gridOptions"
    ></ag-grid-angular>
  `,
  styles: [``],
})
export class DemoBasicComponent {
  gridContext = new WebSiteGridContext(this)
  constructor() {
    this.gridContext.columns = this.columns()
    this.gridContext.rowData = this.generateRowData()
  }

  rowDoubleClick(data) {
    console.log('行双击事件监听', data)
    alert('行双击事件触发')
  }

  primary(cell: AdCellTemplateComponent) {
    console.log(`primary event  handle`, cell)
  }

  danger(cell: AdCellTemplateComponent) {
    console.log(`danger event  handle`, cell)
  }

  checkInfo(cell: AdCellTemplateComponent) {
    console.log(`checkInfo event  handle`, cell)
  }

  edit(cell: AdCellTemplateComponent) {
    console.log(`edit event  handle`, cell)
  }

  delete(cell: AdCellTemplateComponent) {
    console.log(`delete event  handle`, cell)
  }

  download(cell: AdCellTemplateComponent) {
    console.log(`download event  handle`, cell)
  }

  columns() {
    const columns: ColDef[] = [
      WebSiteGridUtils.checkbox(), //选择框渲染
      WebSiteGridUtils.serialNumber(), //序号列渲染
      {
        headerName: 'id',
        field: 'id',
      },
      {
        headerName: 'name',
        field: 'name',
      },
      {
        headerName: 'age',
        field: 'age',
      },
      {
        headerName: '文字操作',
        field: '',
        ...WebSiteGridUtils.cellTemplate(
          WebSiteGridUtils.primary<DemoBasicComponent>({
            click: (cell) => cell.parent.primary(cell),
            value: '查看',
          }),

          WebSiteGridUtils.danger<DemoBasicComponent>({
            click: (cell) => cell.parent.danger(cell),
            value: (cell) => '删除',
          })
        ),
      },
      {
        headerName: '图标操作',
        field: '',
        ...WebSiteGridUtils.cellTemplate(
          WebSiteGridUtils.checkInfo<DemoBasicComponent>({
            auth: 'checkInfo auth',
          }),
          WebSiteGridUtils.edit<DemoBasicComponent>(),
          WebSiteGridUtils.delete<DemoBasicComponent>(),
          WebSiteGridUtils.download<DemoBasicComponent>()
        ),
      },
    ]
    //表格列配置选项合并
    return WebSiteGridUtils.mergeOptions(columns, {
      suppressSizeToFit: true,
      width: 200,
      resizable: true,
      ...WebSiteGridUtils.cellRenderTitle(), //渲染单元格title属性
    })
  }

  generateRowData() {
    return new Array(100).fill(0).map((item, index) => {
      return {
        id: index + 1,
        name: `name-${index + 1}`,
        age: `age-${index + 1}`,
      }
    })
  }
}
